package org.fengling.readingplatform.user.controller;

import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletRequest;
import org.fengling.readingplatform.model.dto.user.UserLoginDto;
import org.fengling.readingplatform.model.dto.user.UserRegisterDto;
import org.fengling.readingplatform.model.entity.user.UserInfo;
import org.fengling.readingplatform.model.vo.common.Result;
import org.fengling.readingplatform.model.vo.common.ResultCodeEnum;
import org.fengling.readingplatform.model.vo.user.UserInfoVo;
import org.fengling.readingplatform.user.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("api/user/userInfo")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @Operation(summary = "用户退出的方法")
    @GetMapping(value = "/logout")
    public Result logout(@RequestHeader(name = "token") String token){
        userInfoService.logout(token);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "获取当前登录用户信息")
    @GetMapping("/auth/getCurrentUserInfo")
    public Result<UserInfoVo> getCurrentUserInfo(HttpServletRequest request){
        String token = request.getHeader("token");
        UserInfoVo userInfoVo = userInfoService.getCurrentUserInfo(token);
        return Result.build(userInfoVo, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "会员登录")
    @PostMapping("/login")
    public Result login(@RequestBody UserLoginDto userLoginDto) {
        String token = userInfoService.login(userLoginDto);
        return Result.build(token, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "会员注册")
    @PostMapping("/register")
    public Result register(@RequestBody UserRegisterDto userRegisterDto) {
        userInfoService.register(userRegisterDto);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "会员信息修改")
    @PutMapping("/auth/updateUserInfo")
    public Result updateUserInfo(@RequestBody UserInfo userInfo){
        UserInfoVo userInfoVo = userInfoService.update(userInfo);
        System.out.println(userInfoVo);
        System.out.println("gggggggggggggggggggggggg");
        return Result.build(userInfoVo, ResultCodeEnum.SUCCESS);
    }
}